<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 表单图标区 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt />
      </div>
      <!-- 表单登录区 -->
      <!-- ref表示表单的实例对象 -->
      <el-form
        class="login_form"
        :model="loginForm"
        :rules="loginFormRules"
        ref="loginFormRef"
        label-width="0px"
      >
        <el-form-item prop="username">
          <!-- v-model数据绑定 -->
          <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user" placeholder="用户名"></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="iconfont icon-3702mima"
            type="password"
            placeholder="密码"
          ></el-input>
        </el-form-item>

        <el-form-item class="btns">
          <el-button type="primary" @click="submitLoginForm">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { async } from 'q'
export default {
  // 业务逻辑定义的数据
  data() {
    return {
      //登录表单数据绑定对象
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      //登录表单验证规则对象
      loginFormRules: {
        username: [
          { required: true, message: '用户名必填', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [{ required: true, message: '密码必填', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 表单重置
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields()
    },
    submitLoginForm() {
      //   this.$refs.loginFormRef.validate((valid, obj) => {
      //     if (!valid) {
      //       return;
      //     }
      //     this.$http.post("login", this.loginForm).then(res => {
      //       if (res.data.meta.status !== 200) {
      //         console.log(res.data.meta.msg);
      //       }
      //     });
      //   });

      this.$refs.loginFormRef.validate(async (valid, obj) => {
        if (!valid) {
          return
        }
        // await只能用在被async修饰的方法中 直接拿到服务器返回的数据
        const { data: result } = await this.$http.post('login', this.loginForm)
        if (result.meta.status !== 200) {
          return this.$message.error(result.meta.msg)
        }
        this.$message.success('登录成功')
        window.sessionStorage.setItem('token', result.data.token)
        // 编程式导航跳转页面
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>